﻿Imports DTO
Imports BUS
Imports DevExpress.XtraEditors

Public Class frmThemKhachHang

    Public Delegate Sub ThemNV(nv As KhachHangDTO)

    Public Event LamMoiNVKhiThem As ThemNV

    Public kt As Integer = 0

    Public maKh As String
    Public tenKh As String
    Public maKV As String
    Public dc As String
    Public dt As String
    Public dd As String
    Public email As String
    Public yahoo As String
    Public skype As String
    Public conQL As Integer
    Public fax As String
    Public mst As String
    Public nh As String
    Public web As String
    Public tk As String



    Private Sub cbxThemKhuVuc_Properties_ButtonClick_1(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxThemKhuVuc.Properties.ButtonClick

        If e.Button.Tag.ToString() = "cbxbtnThemKV" Then
            Dim frm As New frmThemKhuVuc()
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxKhuVuc()
        cbxThemKhuVuc.Properties.DataSource = KhuVucBUS.LayDSKhuVucCbx()
        cbxThemKhuVuc.Properties.ValueMember = "MaKhuVuc"
        cbxThemKhuVuc.Properties.DisplayMember = "TenKhuVuc"
        SetEditValueByIndex(cbxThemKhuVuc, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndex(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxThemKhuVuc.Properties.GetKeyValue(index)
        cbxThemKhuVuc.EditValue = keyValue
    End Sub


    Private Sub frmThemKhachHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCbxKhuVuc()
        If kt = 1 Then
            txtThemMaKH.Text = maKh
            txtThemTenKH.Text = tenKh
            txtThemDiaChi.Text = dc
            txtThemDienThoai.Text = dt
            txtThemEmail.Text = email
            txtThemFax.Text = fax
            txtThemMobile.Text = dd
            txtThemMST.Text = mst
            txtThemNganHang.Text = nh
            txtThemNickSkype.Text = skype
            txtThemNickYahoo.Text = yahoo
            txtThemTK.Text = tk
            If conQL = 1 Then
                chkConQuanLy.Checked = True
            Else
                chkConQuanLy.Checked = False
            End If
            KhachHangBUS.LayMaKhoHangCbx(maKh, maKV)
            cbxThemKhuVuc.EditValue = maKV
        End If
    End Sub

    Sub ThemKhachHang()
        KhachHangBUS.DongKetNoi.Close()
        Dim kh As New KhachHangDTO

        kh.TenKhachHang = txtThemTenKH.Text.Trim()
        kh.MaKhuVuc = cbxThemKhuVuc.EditValue.ToString()
        kh.ChiecKhau = Convert.ToDouble(cbcThemChiecKhau.Text.Trim())
        kh.DiaChi = txtThemDiaChi.Text.Trim()
        kh.DiDong = txtThemMobile.Text.Trim()
        kh.DienThoai = txtThemDienThoai.Text.Trim()
        kh.Email = txtThemEmail.Text.Trim()
        kh.GioiHanNo = Convert.ToDouble(cbcThemGiaHanNo.Text.Trim())
        kh.MaSoThue = txtThemMST.Text.Trim()
        If chkConQuanLy.Checked = True Then
            kh.MaTrangThai = 1
        Else
            kh.MaTrangThai = 0
        End If
        kh.NganHang = txtThemNganHang.Text.Trim()
        kh.NguoiLienHe = txtThemNguoiLienHe.Text.Trim()
        kh.NganHang = txtThemNganHang.Text.Trim()
        kh.NickSkype = txtThemNickSkype.Text.Trim()
        kh.NickYahoo = txtThemNickYahoo.Text.Trim()
        kh.NoHienTai = Convert.ToDouble(cbxThemNoHienTai.Text.Trim())
        kh.TaiKhoan = txtThemTK.Text.Trim()
        kh.TrangWEB = txtThemWebsite.Text.Trim()
        kh.Fax = txtThemFax.Text.Trim()

        If KhachHangBUS.ThemKhachHang(kh) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm thành công khách hàng", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên khách hàng không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        RaiseEvent LamMoiNVKhiThem(kh)
    End Sub

    Sub CapNhatKhachHang()
        KhachHangBUS.DongKetNoi.Close()
        Dim kh As New KhachHangDTO

        kh.MaKhachHang = txtThemMaKH.Text.Trim()
        kh.TenKhachHang = txtThemTenKH.Text.Trim()
        kh.MaKhuVuc = cbxThemKhuVuc.EditValue.ToString()
        kh.ChiecKhau = Convert.ToDouble(cbcThemChiecKhau.Text.Trim())
        kh.DiaChi = txtThemDiaChi.Text.Trim()
        kh.DiDong = txtThemMobile.Text.Trim()
        kh.DienThoai = txtThemDienThoai.Text.Trim()
        kh.Email = txtThemEmail.Text.Trim()
        kh.GioiHanNo = Convert.ToDouble(cbcThemGiaHanNo.Text.Trim())
        kh.MaSoThue = txtThemMST.Text.Trim()
        If chkConQuanLy.Checked = True Then
            kh.MaTrangThai = 1
        Else
            kh.MaTrangThai = 0
        End If
        kh.NganHang = txtThemNganHang.Text.Trim()
        kh.NguoiLienHe = txtThemNguoiLienHe.Text.Trim()
        kh.NganHang = txtThemNganHang.Text.Trim()
        kh.NickSkype = txtThemNickSkype.Text.Trim()
        kh.NickYahoo = txtThemNickYahoo.Text.Trim()
        kh.NoHienTai = Convert.ToDouble(cbxThemNoHienTai.Text.Trim())
        kh.TaiKhoan = txtThemTK.Text.Trim()
        kh.TrangWEB = txtThemWebsite.Text.Trim()
        kh.Fax = txtThemFax.Text.Trim()

        If KhachHangBUS.CapNhatKhachHang(kh) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhật thành công khách hàng", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên khách hàng không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub frmThemKH_btnThem_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnThem.Click
        NhanVienBUS.DongKetNoi.Close()
        If kt = 1 Then
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 18)
            If pq.Sua = True Then
                CapNhatKhachHang()
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý khách hàng", "Cập nhập", maKh)
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Else
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 18)
            If pq.Them = True Then
                ThemKhachHang()
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý khách hàng", "Thêm", maKh)
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

    Private Sub frmThemKH_btnDong_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnDong.Click
        KhachHangBUS.DongKetNoi.Close()
        Me.Close()
    End Sub
End Class